This post is the extension of my previous post - Activity Life Cycle in Android: Starting an Activity . Here we are going to discuss about Pause and resume state in Android Life cycle.
While we are running our app, sometimes the focused foreground activity is obstructed by other visual components which results the activity to pause state. For example, when a semi-transparent activity opens (such as one in the style of dialog), the previous activity gets paused. As long as the activity is still partially visible but currently not the activity in focus, it remains paused.
As our activity enters the paused state, the onPause() method is immediately called by system on our activity, which allow us to stop ongoing actions that should not continue while paused(such as a video) or persist any information that should be permanently saved in case the user continues to leave our app. And if the user returns to our activity from paused state, the system resumes it and calls the onResume() method
When a semi-transparent activity obscures our activity, the system calls onPause() and the activity waits in then paused state (1). If the user returns to the activity while it’s still paused , the system calls onResume()(2)
Pause our Activity
When the system cause onPause() for our activity, its technically means our activity is still partially visible , but most often it’s an indication that the user is leaving the activity and it will soon entered the Stopped state. We should usually use the onPause() callback to :
· Stop animations and other ongoing actions that consumes CPU
· Commit unsaved changes, but only when the user expects such changes to be permanently saved when they leave (such as a draft email).
· Release system resources, such as broadcast receivers, handles to sensors etc.
For example, if our app uses the Camera, the onPause() method is good place to release it.
@Override
public void onPause(){
super.onPause(); // Call to super class method
//Release the Camera because we don’t need it when paused
// and other activates might need to use it
if(mCamera ! = null) {
mCamera.release()
mCamera = null;
}
}
2. We should avoid performing CPU-intensive working during onPause(), such as writing to a database, because it can slow the visible transition to next destination to the next activity
NOTE : When our activity is paused, the activity instance is kept resident in memory and is recalled when the activity resumes. We don’t need to re-initialize components that were created during any of the callbacks methods leading to Resumed state
Resume Our Activity
When the user come back to our activity i.e. resumes our
activity from paused state, the system calls the onResume() method.
That is, system calls this method every time our activity
comes into the foreground, including when it’s created for the first time. As
such we should implement onResume() to initialize our components that we
release during onPause() and perform any other initializations that must occurs
time the activity enters the Resumed state (such as begin animation and initialize components only used while the
activity has user focus).
@Override
public void onResume() {
super.onResume(); //Call the superclass method
// Get the camera instance as the activity achieves full user focus
If(mCamera == null) {
intializeCamera(); // Local method to handle camera init
}
}
Next, we will going to see how to stop and restart an activity in Activity Lifecycle: Stopping and Restarting an Activity in Android
Leave Comment